package com.health.healthSys.service.impl;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.health.common.annotation.DataScope;
import com.health.common.utils.DateUtils;
import com.health.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.health.healthSys.mapper.InfoCommentMapper;
import com.health.healthSys.domain.InfoComment;
import com.health.healthSys.service.IInfoCommentService;

/**
 * 资讯评论Service业务层处理
 *
 * @author maple
 * @date 2025-03-24
 */
@Service
public class InfoCommentServiceImpl implements IInfoCommentService
{
    @Autowired
    private InfoCommentMapper infoCommentMapper;

    /**
     * 查询资讯评论
     *
     * @param commentId 资讯评论主键
     * @return 资讯评论
     */
    @Override
    public InfoComment selectInfoCommentByCommentId(String commentId)
    {
        return infoCommentMapper.selectInfoCommentByCommentId(commentId);
    }

    /**
     * 查询资讯评论列表
     *
     * @param infoComment 资讯评论
     * @return 资讯评论
     */
    @Override
    public List<InfoComment> selectInfoCommentList(InfoComment infoComment)
    {
        return infoCommentMapper.selectInfoCommentList(infoComment);
    }

    /**
     * 新增资讯评论
     *
     * @param infoComment 资讯评论
     * @return 结果
     */
    @Override
    public int insertInfoComment(InfoComment infoComment)
    {
        infoComment.setCreateTime(DateUtils.getNowDate());
        infoComment.setStatus("0"); // 设置初始状态为待审核
        infoComment.setLikeCount(0);
        infoComment.setReportCount(0);
        // 设置当前登录用户ID
        infoComment.setUserId(SecurityUtils.getUserId().toString());
        return infoCommentMapper.insertInfoComment(infoComment);
    }

    /**
     * 修改资讯评论
     *
     * @param infoComment 资讯评论
     * @return 结果
     */
    @Override
    public int updateInfoComment(InfoComment infoComment)
    {
        return infoCommentMapper.updateInfoComment(infoComment);
    }

    /**
     * 批量删除资讯评论
     *
     * @param commentIds 需要删除的资讯评论主键
     * @return 结果
     */
    @Override
    public int deleteInfoCommentByCommentIds(String[] commentIds)
    {
        return infoCommentMapper.deleteInfoCommentByCommentIds(commentIds);
    }

    /**
     * 删除资讯评论信息
     *
     * @param commentId 资讯评论主键
     * @return 结果
     */
    @Override
    public int deleteInfoCommentByCommentId(String commentId)
    {
        return infoCommentMapper.deleteInfoCommentByCommentId(commentId);
    }

    /**
     * 审核评论
     *
     * @param commentId 评论ID
     * @param status 审核状态（1=通过，3=拒绝）
     * @param auditRemark 审核备注
     * @return 结果
     */
    @Override
    public int auditComment(String commentId, String status, String auditRemark)
    {
        InfoComment comment = new InfoComment();
        comment.setCommentId(commentId);
        comment.setStatus(status);
        comment.setAuditRemark(auditRemark);
        comment.setAuditorId(SecurityUtils.getUserId().toString());
        comment.setAuditTime(DateUtils.getTime());
        return infoCommentMapper.updateInfoComment(comment);
    }

    /**
     * 点赞评论
     *
     * @param commentId 评论ID
     * @return 结果
     */
    @Override
    public int likeComment(String commentId)
    {
        InfoComment comment = infoCommentMapper.selectInfoCommentByCommentId(commentId);
        if (comment != null)
        {
            comment.setLikeCount(comment.getLikeCount() + 1);
            return infoCommentMapper.updateInfoComment(comment);
        }
        return 0;
    }

    /**
     * 取消点赞评论
     *
     * @param commentId 评论ID
     * @return 结果
     */
    @Override
    public int unlikeComment(String commentId)
    {
        InfoComment comment = infoCommentMapper.selectInfoCommentByCommentId(commentId);
        if (comment != null && comment.getLikeCount() > 0)
        {
            comment.setLikeCount(comment.getLikeCount() - 1);
            return infoCommentMapper.updateInfoComment(comment);
        }
        return 0;
    }

    /**
     * 举报评论
     *
     * @param commentId 评论ID
     * @param reportReason 举报原因
     * @return 结果
     */
    @Override
    public int reportComment(String commentId, String reportReason)
    {
        InfoComment comment = infoCommentMapper.selectInfoCommentByCommentId(commentId);
        if (comment != null)
        {
            comment.setReportCount(comment.getReportCount() + 1);
            comment.setReportReason(reportReason);
            return infoCommentMapper.updateInfoComment(comment);
        }
        return 0;
    }

    /**
     * 获取评论树形结构
     *
     * @param infoId 资讯ID
     * @return 评论树形结构
     */
    @Override
    public List<InfoComment> getCommentTree(String infoId)
    {
        InfoComment query = new InfoComment();
        query.setInfoId(infoId);
        // 修改查询条件，同时显示已发布和待审核的评论
        List<InfoComment> allComments = infoCommentMapper.selectInfoCommentList(query);

        // 构建树形结构
        List<InfoComment> rootComments = allComments.stream()
            .filter(comment -> comment.getParentId() == null || comment.getParentId().isEmpty())
            .collect(Collectors.toList());

        for (InfoComment root : rootComments)
        {
            root.setChildren(getChildren(root.getCommentId(), allComments));
        }

        return rootComments;
    }

    /**
     * 获取待审核评论列表
     *
     * @param infoComment 评论查询条件
     * @return 待审核评论列表
     */
    @Override
    public List<InfoComment> selectPendingAuditList(InfoComment infoComment)
    {
        infoComment.setStatus("0"); // 设置查询条件为待审核状态
        return infoCommentMapper.selectInfoCommentList(infoComment);
    }

    /**
     * 递归获取子评论
     *
     * @param parentId 父评论ID
     * @param allComments 所有评论
     * @return 子评论列表
     */
    private List<InfoComment> getChildren(String parentId, List<InfoComment> allComments)
    {
        List<InfoComment> children = allComments.stream()
            .filter(comment -> parentId.equals(comment.getParentId()))
            .collect(Collectors.toList());

        for (InfoComment child : children)
        {
            child.setChildren(getChildren(child.getCommentId(), allComments));
        }

        return children;
    }
}
